{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 函数\n",
    " - 一个函数完成一个功能，一段代码的组织形式\n",
    " - 一般不需要太长\n",
    " - 使用\n",
    "     - 先定义   def 函数名（参数可选）\n",
    "     - 在调用   函数名（参数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i am power and i love power\n"
     ]
    }
   ],
   "source": [
    "#函数定义\n",
    "def func(person):\n",
    "    return \"i am {0} and i love {0}\".format(person)\n",
    "#函数调用\n",
    "p=\"power\"\n",
    "print(func(p))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 \n",
      "\n",
      "2 4 \n",
      "\n",
      "3 6 9 \n",
      "\n",
      "4 8 12 16 \n",
      "\n",
      "5 10 15 20 25 \n",
      "\n",
      "6 12 18 24 30 36 \n",
      "\n",
      "7 14 21 28 35 42 49 \n",
      "\n",
      "8 16 24 32 40 48 56 64 \n",
      "\n",
      "9 18 27 36 45 54 63 72 81 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "#九九乘法表\n",
    "for i in range(1,10):\n",
    "    for j in range(1,i+1):\n",
    "        if i>=j:\n",
    "            print (i*j,end=' ')\n",
    "    else: \n",
    "        print('\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name power\nage 20\n"
     ]
    }
   ],
   "source": [
    "var={'name':'power','age':20}\n",
    "for index,value in var.items():\n",
    "    print(index,value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 函数参数类型\n",
    "- 普通参数：按照形参对实参一一对应，但是实参是可以多放入不可以少放入的\n",
    "- 默认参数：可以不传入参数，函数默认有值\n",
    "- 关键字参数：类似默认参数，但是可以不按照参数顺序传而是按照key=value传入\n",
    "- 收集参数：def func(*args),把传入的参数当作元组储存起来，两个型号的话是用字典来储存变量\n",
    "- tips:在使用key=value传入值时，key不用加上引号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "下面是一段自我介绍\n",
      "power\n",
      "21\n",
      "single\n"
     ]
    }
   ],
   "source": [
    "#收集参数的例子\n",
    "def stu(*args):\n",
    "    print('下面是一段自我介绍')\n",
    "    for index in args:\n",
    "        print(index)\n",
    "stu('power',21,'single')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "my name is power and i am 21\n"
     ]
    }
   ],
   "source": [
    "# 关键字参数例子，可以不按照顺序\n",
    "def func(name,age):\n",
    "    print(\"my name is {0} and i am {1}\".format(name,age))\n",
    "func(age=21,name=\"power\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "下面是一段自我介绍\n",
      "name ------ power\n",
      "age ------ 21\n",
      "status ------ single\n"
     ]
    }
   ],
   "source": [
    "#收集参数之关键字参数\n",
    "def stu( ** args):\n",
    "    print('下面是一段自我介绍')\n",
    "    for k,v in args.items():\n",
    "        print(k,'------',v)\n",
    "stu(name='power',age=21,status='single')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "power\n",
      "17\n",
      "single\n"
     ]
    }
   ],
   "source": [
    "# 传入一个list的时候，需要使用*进行解包\n",
    "def stu(*var):\n",
    "    for index in var:\n",
    "        print(index)\n",
    "var=['power',17,'single']\n",
    "stu(*var)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 函数的返回值：如果没有返回值，默认就是none"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 函数文档\n",
    "- 使用三引号注释来说明函数的用法以及返回值\n",
    "- 使用help(func)或者func.__doc__来查看函数的文档"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function stu in module __main__:\n",
      "\n",
      "stu(name, age)\n",
      "    这是函数的说明\n",
      "    这是函数的用法以及返回值说明\n",
      "\n",
      "********************\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\n    这是函数的说明\\n    这是函数的用法以及返回值说明\\n    '"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def stu(name,age):\n",
    "    '''\n",
    "    这是函数的说明\n",
    "    这是函数的用法以及返回值说明\n",
    "    '''\n",
    "    pass\n",
    "help(stu)\n",
    "print(\"*\"*20)\n",
    "stu.__doc__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
